//
//  UIAlertView+Block.m
//  vidpioneer
//
//  Created by pcyang on 15/5/15.
//  Copyright (c) 2015年 tencent. All rights reserved.
//

#import "UIAlertView+Block.h"
#import <objc/runtime.h>

static void *AlertClickedBlockKey = &AlertClickedBlockKey;

@implementation UIAlertView (Block)
- (void)setClickedButtonBlock:(AlertViewClickBlock)clickedButtonBlock {
    
    if (self.delegate != self) {
        self.delegate = self;
    }
    
    objc_setAssociatedObject(self, &AlertClickedBlockKey, clickedButtonBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (AlertViewClickBlock)clickedButtonBlock {
    
    return objc_getAssociatedObject(self, &AlertClickedBlockKey);
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (self.clickedButtonBlock) {
        
        __weak typeof(self) weakSelf = self;
        self.clickedButtonBlock(weakSelf, buttonIndex);
        self.clickedButtonBlock = nil;
    }
    
}
@end
